package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateUnion;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_ELIMINATE_UNION_STATUS, desc = "")
public class CrossStrongholdUnionStatusCmd extends CrossUnionCommand {
    private int activityId;
    private String unionId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdEliminateUnionStatusReqMsg req = StrongholdWarProto.CrossStrongholdEliminateUnionStatusReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        unionId = req.getUnionId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        boolean exist = false;
        boolean allFailed = true;
        List<CrossStrongholdEliminateUnion> eliminateUnionList = CrossStrongholdMgr.getEliminateUnionList(activityId, groupId);
        for (CrossStrongholdEliminateUnion union : eliminateUnionList) {
            if (union.getUnionUid().equals(unionId) && union.getCurBlood().compareTo(BigInteger.ZERO) > 0) {
                exist = true;
            }
        }

        for (CrossStrongholdEliminateUnion union : eliminateUnionList) {
            if (!union.getUnionUid().equals(unionId) && union.getCurBlood().compareTo(BigInteger.ZERO) > 0) {
                allFailed = false;
            }
        }

        StrongholdWarProto.CrossStrongholdEliminateUnionStatusRespMsg.Builder resp = StrongholdWarProto.CrossStrongholdEliminateUnionStatusRespMsg.newBuilder();
        resp.setActivityId(activityId);
        resp.setUnionId(unionId);
        resp.setExist(!allFailed && exist);
        resp.setUserId(packet.getPlayerId());
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_STRONGHOLD_ELIMINATE_UNION_STATUS, resp);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
